<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
    <template v-slot:object="{row}">
      <table class="object-info">
        <tbody>
        <tr>
          <td>项目主体</td>
          <td :colspan="row.value.additional_select_map ? Object.keys(row.value.additional_select_map).length : 1">
            <div class="flex" style="width: 100%;align-items: center;justify-content: center;">
              <img style="height: 36px;" :src="row.value.head_img" />
              <p>{{row.value.title}}</p>
              <p style="margin-left: 2px;color: var(--fail);">￥<b>{{row.value.price-row.value.additional_price}} <span v-if="row.value.deliver_price>0">+{{row.value.deliver_price}}(运费)</span></b></p>
            </div>
          </td>
          <td rowspan="2">数量x{{row.value.num}}</td>
        </tr>
        <tr v-if="row.value.additional_select_map">
          <td>定制配置</td>
          <td v-for="v in row.value.additional_select_map">
            <div class="flex" style="align-items: center;justify-content: center;">
              <img style="height: 36px;" :src="v.head_img" />
              <p>{{v.title}}</p>
              <p style="margin-left: 2px;color: var(--fail);">总价￥<b>{{v.sum_price}}</b></p>
              <p>（数量x{{v.num}}）</p>
            </div>
          </td>
        </tr>
        </tbody>
      </table>
    </template>
    <template v-slot:extend="{row}">
      <div v-if="row.value?.additional_select_map">
        <h3>定制配置</h3>
        <ul class="flex" style="flex-wrap: wrap;">
          <li v-for="v in row.value.additional_select_map" class="flex" style="width: auto;align-items: center; margin: 2px;">
            <img style="height: 36px;" :src="v.head_img" />
            <p>{{v.title}}</p>
            <p style="margin-left: 2px;color: var(--fail);">总价￥<b>{{v.sum_price}}</b></p>
            <p>（数量x{{v.num}}）</p>
          </li>
        </ul>
      </div>
    </template>
    <template v-slot:order_pay_promotion_info="{row}">
      <div class="flex">
        <p style="margin-right: 4px;">{{row.value.title}}</p>
        <p v-if="row.value.reduction_type==0" style="color: var(--fail);">减免 {{row.value.reduction_value}} 元</p>
        <p v-if="row.value.reduction_type==1" style="color: var(--fail);">折扣 {{row.value.reduction_value}}%</p>
        <p v-if="row.value.reduction_type==2" style="color: var(--fail);">项目价格降低至 {{row.value.reduction_value}} 元</p>
      </div>
    </template>
    <template v-slot:refund_info="{row}">
      <div style="width: 100%;text-align: left;" v-if="row.value.order_refund_id">
        <div class="flex">
          <span>状态：</span>
          <select_one :config="row.config" :value="row.value.order_refund_status" is_info="1"></select_one>
        </div>
        <p>申请时间：{{row.value.order_refund_create_time}}</p>
        <p>退款类型：{{row.value.order_refund_title}}</p>
        <p>退款理由：{{row.value.order_refund_reason_user}}</p>
        <p>退款金额：{{row.value.order_refund_refund_money}}</p>
        <p>方式：{{row.value.order_refund_need_deliver?'退货退款':'直接退款'}}</p>
        <p v-if="row.value.order_refund_status>=2" >管理员备注：{{row.value.order_refund_reason_admin}}</p>
        <p v-if="row.value.order_refund_status==5" >退款时间：{{row.value.refund_time}}</p>
      </div>
    </template>
    <template v-slot:hr="{row}">
      ---------------------------{{row.value}}---------------------------
    </template>

    <template #kickback_list="{row}">
      <list :data="row.value" :cols="[{name:'nickname',label:'用户昵称'}, {name:'title',label:'佣金类型'}, {name:'kickback_ratio',label:'比例'}, {name:'money',label:'金额'}]"></list>
    </template>

  </forms>
</template>

<script>
export default {
  components: {},

  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let model = {name:this.$get('model_name'), alias:this.$get('model_alias','')};

    return {
      id: id,
      btns: btns,
      model: model,
      config: {},
    }
  },
  methods: {
    param(data){
      data = data || {};
      data.model_name = this.model.name;
      data.model_alias = this.model.alias;
      return s94Mjs.param_encode(data);
    },
    in_right_action(action){
      return in_right_admin(this.camel_str(this.model.name)+'/'+action);
    },
    onsubmit(formData){
      let url = this.id ? `/admin/${this.model.name}/order_edit?id=`+this.id : '/admin/_base_/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('index?'+this.param(), '$refs.list_url.list');
          iframe.hide('edit?'+this.param());
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index?'+this.param());
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    },
    get_info(){
      if (!this.id) return;
      ajax({url:'/admin/'+this.model.name+'/order_info?id='+this.id}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      })
    },
    kickback_list(id){
      if (!in_right_admin("UserKickback/list")) return;
      let where = {
        'where[link_table][eq]': this.model.name+'_order',
        'where[link_id][eq]': id,
      }
      ajax({url:'/admin/user_kickback/list',data:where}, (res)=>{
        let list = res.data.list;
        this.config.kickback_list.remove = false;
        this.config.kickback_list.value = list;
        return false;
      })

    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/'+app.model.name+'/order_info?get_field_config=1&id='+this.id, cache:true}, (res)=>{
        app.model = res.field_config.model;
        let extend = JSON.parse(res.data.extend);
        let additional_price = 0;
        if (extend.additional_select_map){
          s94Mjs.each(extend.additional_select_map, function (v){ additional_price += v.sum_price; })
        }
        res.data.object = {
          title: res.data.title,
          head_img: res.data.head_img,
          num: res.data.num,
          price: res.data.price,
          deliver_price: res.data.deliver_price,
          additional_select_map: extend.additional_select_map,
          additional_price: additional_price,
        }
        res.data.refund_info = {};
        s94Mjs.each(res.data, function (v,k){
          if (k.indexOf('order_refund_')===0) res.data.refund_info[k]=v;
        })
        res.data.refund_info['refund_time']=res.data.refund_time;
        let skip_deliver = res.data.skip_deliver; //跳过发货
        let status_map = app.admin_config('status_alias_map.order.status');
        let action_status_map = {deliver:2, take:3, use:4};
        let action_alias = app.model.action_alias || {};
        Object.keys(action_alias).forEach((k)=>{
          if (!action_status_map[k]) return;
          status_map[action_status_map[k]] = '待'+action_alias[k];
        })
        res.data.status_label = status_map[res.data.status];
        //表单配置
        let forms_config = {
          user_nickname: {label:'用户昵称',name:'user_nickname'},
          user_phone: {label:'用户电话',name:'user_phone'},
          deliver_way: {label:'发货方式',name:'deliver_way', type:'hidden', config:'快递|0,自提|1', remove:!!skip_deliver},
          deliver_user: {label:'物流联系人',name:'deliver_user', remove:!!skip_deliver},
          deliver_phone: {label:'物流联系电话',name:'deliver_phone', remove:!!skip_deliver},
          deliver_address: {label:'物流地址',name:'deliver_address', remove:!!skip_deliver},
          deliver_no: {label:'物流单号',name:'deliver_no', remove:!!skip_deliver},

          hr1: {label:'',type:'hr',name:'',value:'以下为下单数据'},
          id: {label:'订单ID',name:'id'},
          order_pay_order_no: {label:'订单编号',name:'order_pay_order_no'},
          object: {label:app.model.alias+'详情',name:'object',type:'object',value:{}},
          order_pay_card_id: {label:'卡券',name:'order_pay_card_id'},
          order_pay_promotion_info: {label:'优惠活动',name:'order_pay_promotion_info',type:'order_pay_promotion_info',value:{}},
          pay_money: {label:'支付金额',name:'pay_money'},
          order_pay_pay_type: {label:'支付方式',name:'order_pay_pay_type',type:'select_one',is_info:true,config:app.admin_config('status_alias_map.order_pay.pay_type',[])},
          status: {label:'状态',name:'status',type:'select_one',is_info:true,config:app.admin_config('status_alias_map.order.status'), remove:true},
          status_label: {label:'状态',name:'status_label'},

          kickback_list: {label:"分佣情况",name:'kickback_list',type:'kickback_list',value:[], remove:true},

          hr2: {label:'',type:'hr',name:'',value:'以下为时间数据'},
          create_time: {label:'下单时间',name:'create_time'},
          pay_time: {label:'支付时间',name:'pay_time'},
          deliver_time: {label:'发货时间',name:'deliver_time'},
          use_time: {label:'核销时间',name:'use_time'},
          over_time: {label:'成交时间',name:'over_time'},

          hr3: {label:'',type:'hr',name:'',value:'以下为退款数据'},
          refund_info: {label:'退款情况',name:'refund_info',type:'refund_info',value:{},config:app.admin_config('status_alias_map.order_refund.status')},

          hr4: {label:'',type:'hr',name:'',value:'以下为其他数据'},
        }

        let is_info = app.$get('is_info');
        s94Mjs.each(res.field_config.cols, function(v){
          v.name = v.name.replace('.','_');
          let row = {
            label: v.label,
            name: v.name,
            tips: v.tips,
            type: v.type,
            is_info: v.can_edit!=1 || is_info,
            config: v.config,
            value: v.value,
          };
          forms_config[v.name] = Object.assign((forms_config[v.name] || {}), row);
        })
        if (res.data.deliver_way == 1) {
          forms_config.deliver_user.label = '自提点联系人';
          forms_config.deliver_phone.label = '自提点联系电话';
          forms_config.deliver_address.label = '自提点地址';
          forms_config.deliver_no.remove = true;
        }
        if (is_info) app.btns = [];
        app.config = forms_config;
        app.kickback_list(this.id);
        setTimeout(function(){ app.$refs['forms'].init(res.data); });
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('edit?'+this.param());
        }else {
          this.$router.push('index?'+this.param());
        }
      })
    }

    //其他初始化操作
  }
}
</script>
<style>
.object-info td{border: 1px solid #333;}
</style>